const Response = require('../utils/response');
const adminService = require('../services/adminService');

class AdminController {
  // 管理员登录
  async login(req, res) {
    try {
      const { username, password, remember } = req.body;
      const result = await adminService.login(username, password, remember);
      res.json(Response.success(result, '登录成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 获取管理员列表
  async getAdminList(req, res) {
    try {
      const admins = await adminService.getAdminList();
      res.json(Response.success(admins));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 添加管理员
  async addAdmin(req, res) {
    try {
      const adminId = await adminService.addAdmin(req.body);
      res.json(Response.success({ id: adminId }, '添加成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 更新当前管理员密码
  async updatePassword(req, res) {
    try {
      const { oldPassword, newPassword } = req.body;
      await adminService.updatePassword(req.user.id, oldPassword, newPassword);
      res.json(Response.success(null, '密码修改成功'));
    } catch (error) {
      if (error.status === 400) {
        res.status(400).json(Response.error(error.message, 400));
      } else {
        res.status(500).json(Response.error(error.message, 500));
      }
    }
  }

  // 更新管理员信息
  async updateAdmin(req, res) {
    try {
      await adminService.updateAdmin(req.params.id, req.body);
      res.json(Response.success(null, '更新成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

  // 删除管理员
  async deleteAdmin(req, res) {
    try {
      await adminService.deleteAdmin(req.params.id);
      res.json(Response.success(null, '删除成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 获取用户列表
  async getUserList(req, res) {
    try {
      const users = await adminService.getUserList();
      res.json(Response.success(users));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 创建用户
  async addUser(req, res) {
    try {
      const userId = await adminService.addUser(req.body);
      res.json(Response.success({ id: userId }, '添加成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 更新用户信息
  async updateUser(req, res) {
    try {
      await adminService.updateUser(req.params.id, req.body);
      res.json(Response.success(null, '更新成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 删除用户
  async deleteUser(req, res) {
    try {
      await adminService.deleteUser(req.params.id);
      res.json(Response.success(null, '删除成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 更新用户状态
  async updateUserStatus(req, res) {
    try {
      await adminService.updateUserStatus(req.params.id, req.body.status);
      res.json(Response.success(null, '状态更新成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 获取用户发布的书籍
  async getUserPosts(req, res) {
    try {
      const posts = await adminService.getUserPosts(req.params.id);
      res.json(Response.success(posts));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

//   // 获取待审核的书籍列表
//   async getPendingBooks(req, res) {
//     try {
//       const books = await adminService.getPendingBooks();
//       res.json(Response.success(books));
//     } catch (error) {
//       res.json(Response.error(error.message));
//     }
//   }

//   // 审核书籍
//   async reviewBook(req, res) {
//     try {
//       const { action, rejectReason } = req.body;
//       if (action === 'approve') {
//         await adminService.approveBook(req.params.id, req.user.id);
//         res.json(Response.success(null, '审核通过'));
//       } else if (action === 'reject') {
//         await adminService.rejectBook(req.params.id, req.user.id, rejectReason);
//         res.json(Response.success(null, '已驳回'));
//       } else {
//         res.json(Response.error('无效的操作'));
//       }
//     } catch (error) {
//       res.json(Response.error(error.message));
//     }
//   }

     // 更新管理员权限
  async updatePermissions(req, res) {
    try {
      await adminService.updateAdminPermissions(
        parseInt(req.params.id),
        req.body.permissions
      );
      res.json(Response.success(null, '权限更新成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }
}

module.exports = new AdminController(); 